package org.lagou.Dao.impl;

import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.lagou.Dao.UserDao;
import org.lagou.bean.UserBean;

public class UserDaoImpl extends BaseDao implements UserDao {

	@Override
	public List<UserBean> getUserAll() {
		try {
			return query.query("select * from user",  new BeanListHandler<UserBean>(UserBean.class));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<UserBean> getUserPagin(int pageIndex, int pageSize) {
		try {
			return query.query("select * from user limit ?,?",  new BeanListHandler<UserBean>(UserBean.class),(pageIndex-1)*pageSize,pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getUserCount() {
		try {
			return query.query("select count(1) from user where type!=3", new ScalarHandler<Long>());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	@Override
	public List<UserBean> getByEmailUser(String email) {
		try {
			return query.query("select * from user where email=?",  new BeanListHandler<UserBean>(UserBean.class),email);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public int addUser(UserBean info) {
		try {
			return query.update("insert into user(name,pwd,tel,type,state,email,del,date) values(?,?,?,?,?,?,?,?)",info.getName(),info.getPwd(),info.getTel(),info.getType(),info.getState(),info.getEmail(),info.getDel(),info.getDate());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateUser(UserBean info) {
		try {
			return query.update("update user set name=?,pwd=?,tel=?,type=?,state=?,email=?,del=?,date=? where id=?",info.getName(),info.getPwd(),info.getTel(),info.getType(),info.getState(),info.getEmail(),info.getDel(),info.getDate(),info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int deleteUserById(int id) {
		try {
			return query.update("delete from user where id=?",id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updatePwdById(UserBean info) {
		try {
			return query.update("update user set pwd=? where id=?",info.getPwd(),info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<UserBean> getUserByName1(String name,int type) {
		try {
			return query.query("select * from user where user.name like ? and type!=?",  new BeanListHandler<UserBean>(UserBean.class),"%"+name+"%",type);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<UserBean> getUserByType(int type) {
		try {
			return query.query("select * from user where type=?",  new BeanListHandler<UserBean>(UserBean.class),type);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int updateDelById(UserBean info) {
		try {
			return query.update("update user set del=? where id=?",info.getDel(),info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<UserBean> getUserOutType(int type,int pageIndex, int pageSize) {
		try {
			return query.query("select * from user where type!=? limit ?,?",  new BeanListHandler<UserBean>(UserBean.class),type,(pageIndex-1)*pageSize,pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int addAdmin(UserBean info) {
		try {
			return query.update("insert into user(name,pwd,tel,type,email,del) values(?,?,?,?,?,?)",info.getName(),info.getPwd(),info.getTel(),3,info.getEmail(),0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<UserBean> getUserByName2(String name, int type) {
		try {
			return query.query("select * from user where user.name like ? and type=?",  new BeanListHandler<UserBean>(UserBean.class),"%"+name+"%",type);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int adduser(UserBean info) {
		try {
			return query.update("insert into user(name,pwd,tel,type,email,del) values(?,?,?,?,?,?)",info.getName(),info.getPwd(),info.getTel(),info.getType(),info.getEmail(),0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateUserName(UserBean info) {
		try {
			return query.update("update user set name=?,date=? where id=?",info.getName(),info.getDate(),info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	
}
	
	
	
